#  This file is part of FlickFleck.
#  Copyright (C) 2008 Jyke Tapani Jokinen
#
#    FlickFleck is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    FlickFleck is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with FlickFleck.  If not, see <http://www.gnu.org/licenses/>.
#
import sys
import config
import ui
import os, os.path
import sys


#
# read a configuration file and set values to config-module
#
def readconfig( file, mod ):
    try:
        f = open( file )
        for line in f.readlines():
            line = line.strip()
            if len(line) < 1:
                continue       # skip blank lines
            if line[0] == '#':
                continue       # comment lines
            try:
                name, val = line.split('=', 1)
                setattr( mod, name.strip(), eval( val.strip() ) )
            except ValueError:
                ui.ERR( "Unknown line in config-file (%s)" % line )
        f.close()
    # any error in processing config datafile is silently ignored
    # (defaults are used instead)
    except IOError:
        pass

#
# called at program startup to initialize the configuration
#
def init():
    mod = sys.modules['config']
    readconfig( 'config.txt', mod )
    readconfig( 'config.local', mod )
    if sys.platform == 'darwin':
        readconfig( _mac_confpath(), mod )
    
    # executables
    if not hasattr( mod, 'jpegtran' ):
        if sys.platform == 'win32':
            setattr(mod, 'jpegtran', "jpegtran.exe")
        else:
            setattr(mod, 'jpegtran', "jpegtran" )


    if not hasattr( mod, 'exiftool' ):
        if sys.platform == 'win32':
            setattr(mod, 'exiftool', "exiftool.exe" )
        else:
            setattr(mod, 'exiftool', "exiftool" )


    # these cannot be set in the configuration file
    setattr(mod, 'TITLE', 'FlickFleck')
    setattr(mod, 'VERSION', '1.0')

    # if program was started with a parameter, use that as fromdir
    if len(sys.argv) > 1:
        setattr(mod, 'fromdir', sys.argv[1])

    # try to sanitize the filepath values
    import os.path
    setattr(mod, 'fromdir', os.path.normpath( config.fromdir ) )
    setattr(mod, 'todir'  , os.path.normpath( config.todir ) )

    if sys.platform == 'darwin':
        mac_initconfig()

# location where we want to store config in macosX
def _mac_confpath():
    if sys.platform == 'darwin':
        return os.environ["HOME"] + "/Library/Preferences/FlickFleck.txt"
    else:
        return "NOSUCHFILE" # should not happen, but wont raise error here

# if we don't have a config in Preferences (macosX) go and create it
def mac_initconfig():
    if sys.platform == 'darwin':
        if not os.path.exists( _mac_confpath() ):
            import shutil
            shutil.copy( config.installdir + "/config.txt", _mac_confpath() )

# simple way to allow user to edit the config (macosX)
def mac_editconfig():
    if sys.platform == 'darwin':
        # I'm real happy that macosX will find the editor for me
        os.system( "open " + _mac_confpath() ) 





